using System;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using Ditto.Helpers;
using Ditto.Models;
using Ditto.Services;

namespace Ditto.Controllers
{

    public class JsonItem
    {
        public int ItemID { get; set; }
        public DateTime EventDate { get; set; }
        public string Title { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string Description { get; set; }
        public int RequestCount { get; set; }
        public string Url { get; set; }

        public string PostingUserID { get; set; }
        public DateTime TimeCreated { get; set; }
    }

    [HandleErrorWithELMAH]
    public class SearchController : Controller
    {

        IItemRepository itemRepository;

        //
        // Dependency Injection enabled constructors

        public SearchController()
            : this(new ItemRepository())
        {
        }

        public SearchController(IItemRepository repository)
        {
            itemRepository = repository;
        }

        //
        // AJAX: /Search/FindByLocation?longitude=45&latitude=-90

        [HttpPost]
        public ActionResult SearchByLocation(float latitude, float longitude)
        {
            var items = itemRepository.FindByLocation(latitude, longitude);

            var jsonItems = from item in items.AsEnumerable()
                              select JsonItemFromItem(item);

            return Json(jsonItems.ToList());
        }

        [HttpPost]
        public ActionResult SearchByPlaceNameOrZip(string placeOrZip)
        {
            if (String.IsNullOrEmpty(placeOrZip)) return null; ;
            LatLong location = GeolocationService.PlaceOrZipToLatLong(placeOrZip);

            var items = itemRepository.
                            FindByLocation(location.Lat, location.Long).
                            OrderByDescending(p => p.EventDate);

            return View("Results", new PaginatedList<Item>(items, 0, 20));
        }

     
        //
        // AJAX: /Search/GetMostPopularItems
        // AJAX: /Search/GetMostPopularItems?limit=5

        [HttpPost]
        public ActionResult GetMostPopularItems(int? limit)
        {
            var items = itemRepository.FindUpcomingItems();

            // Default the limit to 40, if not supplied.
            if (!limit.HasValue)
                limit = 40;

            var mostPopularItems = from item in items
                                     //orderby item.RSVPs.Count descending
                                     select item;

            var jsonItems =
                mostPopularItems.Take(limit.Value).AsEnumerable()
                .Select(item => JsonItemFromItem(item));

            return Json(jsonItems.ToList());
        }

        private JsonItem JsonItemFromItem(Item item)
        {
            return new JsonItem
            {
                ItemID = item.ItemID,
                EventDate = item.EventDate,
                Latitude = item.Latitude,
                Longitude = item.Longitude,
                Title = item.Title,
                Description = item.Description,
                RequestCount = item.Requests.Count,

                //TODO: Need to mock this out for testing...
                //Url = Url.RouteUrl("PrettyDetails", new { Id = item.ItemID } )
                Url = item.ItemID.ToString(),

                PostingUserID = item.PostingUserID,
                TimeCreated = item.TimeCreated
                
            };
        }

    }
}